package com.exchange.service.impl;

import com.exchange.service.TransService;
import com.exchange.utils.result.CommonResult;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.repository.Repository;
import org.pentaho.di.repository.RepositoryDirectoryInterface;
import org.pentaho.di.repository.kdr.KettleDatabaseRepository;
import org.pentaho.di.trans.Trans;
import org.pentaho.di.trans.TransMeta;
import org.springframework.stereotype.Service;

@Service
public class TransServiceImpl implements TransService {


    @Override
    public CommonResult executeDataBaseRepTran(KettleDatabaseRepository repository, String transName, String[] param, String jgname) throws KettleException {
        //根据变量查找到模型所在的目录对象,此步骤很重要。
        RepositoryDirectoryInterface directory = repository.findDirectory("/");
        //创建ktr元对象
        TransMeta transformationMeta = ((Repository) repository).loadTransformation(transName, directory, null, true, null);
        //创建ktr
        Trans trans = new Trans(transformationMeta);
        //执行ktr
        trans.execute(param);
        //等待执行完毕
        trans.waitUntilFinished();

        if (trans.getErrors() > 0) {
            return CommonResult.fail("转换执行失败");
        } else {
            return CommonResult.success("转换执行成功");
        }
    }
}
